﻿using System.Linq;
using System.Web.Mvc;
using AtentoFramework2008.Infrastructure.Entity;
using NStore.Domain;
using NStore.Domain.Violations;
using NStore.Web.Helpers;
using NStore.Web.Models;

namespace NStore.Web.Controllers
{
    public class ProductController : Controller
    {
        private readonly Repository<Product> _repositoryProduct;

        public ProductController() : this(new Repository<Product>()) { }

        public ProductController(Repository<Product> repositoryProduct)
        {
            _repositoryProduct = repositoryProduct;
        }

        //
        // GET: /Product/
        public ActionResult Index()
        {
            return View(_repositoryProduct);
        }

        //
        // GET: /Product/Details/22345
        public ActionResult Details(int id)
        {
            Product product = _repositoryProduct[id];
            return product == null ? View("NotFound") : View("Details", product);
        }


        //
        // GET: /Product/Create
        [Authorize(Roles = "admin")]
        public ActionResult Create()
        {
            return View(new ProductFormViewModel(new Product()));
        }

        //
        // POST: /Product/Create
        /* 
         * public ActionResult Create([Bind(Exclude = "Id")]Product product)
         */
        [AcceptVerbs(HttpVerbs.Post), Authorize(Roles = "admin")]
        public ActionResult Create(Product product)
        {
            product.Validator = new ProductValidator(_repositoryProduct);
            ProductFormViewModel productFormViewModel = new ProductFormViewModel(product);

            if (ModelState.IsValid)
            {
                if (product.IsValid())
                {
                    _repositoryProduct.Add(product);
                    return RedirectToAction("Index");
                }
            }
            ModelState.AddRuleViolations(productFormViewModel.GetRuleViolations());

            return View(productFormViewModel);
        }

        //
        // GET: /Product/Edit/21312
        [Authorize(Roles = "admin")]
        public ActionResult Edit(int id)
        {
            return View(new ProductFormViewModel(_repositoryProduct[id]));
        }
        
        //
        // POST: /Product/Edit/21312
        [AcceptVerbs(HttpVerbs.Post), Authorize(Roles = "admin")]
        public ActionResult Edit(Product product, int id)
        {
            product.Validator = new ProductValidator(_repositoryProduct.Where(item => item.Id != id));
            ProductFormViewModel productFormViewModel = new ProductFormViewModel(product);
            if (ModelState.IsValid)
            {
                if (product.IsValid())
                {
                    PopulateData(product, id);
                    return RedirectToAction("Index");
                }
            }
            ModelState.AddRuleViolations(productFormViewModel.GetRuleViolations());
            return View(productFormViewModel);
        }

        //
        // HTTP GET: /Product/Delete/21312
        [Authorize(Roles = "admin")]
        public ActionResult Delete(int id)
        {
            Product product = _repositoryProduct[id];
            if (product == null)
                return View("NotFound");
            return View(product);
        }

        //
        // HTTP POST: /Dinners/Delete/21312
        [AcceptVerbs(HttpVerbs.Post), Authorize(Roles = "admin")]
        public ActionResult Delete(int id, string confirmbutton)
        {
            Product product = _repositoryProduct[id];
            if (product == null)
                return View("NotFound");
            _repositoryProduct.Remove(product);
            return View("Deleted");
        }

        private void PopulateData(Product productView, int id)
        {
            Product product = _repositoryProduct[id];
            product.Name = productView.Name;
            product.Quantity = productView.Quantity;
            product.Description = productView.Description;
            product.Category = productView.Category;
        }

    }
}